﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "GbmpDrawArcBy3PtsModelLineWithoutRefCmd.h"
#include "GbmpUiPlatformCommandRegister.h"
#include "GbmpUiPlatformCommandIds.h"
#include "CmdStatusUtils.h"
#include "ActionDrawArcBy3PtsElementInput.h"
#include "ActionDrawArcBy3PtsElementCreator.h"
#include "GbmpSnapEndPointPriorityTypesPreprocessor.h"
#include "AddGbmpDynamicPannelUtils.h"
#include "GbmpControlIds.h"
#include "IUserTransaction.h"
#include "IModelView.h"
#include "IModelLine.h"
#include "IArc3d.h"
#include "IElementBasicInformation.h"
#include "UiDocumentViewUtils.h"

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;

REGISTER_GM_COMMAND(GbmpDrawArcBy3PtsModelLineWithoutRefCmd)

GbmpDrawArcBy3PtsModelLineWithoutRefCmd::GbmpDrawArcBy3PtsModelLineWithoutRefCmd()
    : GbmpCommandBase(ID_CMD_GBMP_DRAW_ARC_3PTS_MODEL_LINE_WITHOUT_REF)
{
}

GbmpDrawArcBy3PtsModelLineWithoutRefCmd::~GbmpDrawArcBy3PtsModelLineWithoutRefCmd()
{
}

bool GbmpDrawArcBy3PtsModelLineWithoutRefCmd::IsVisible() const
{
    CMD_ONLY_VISIBLE_IN_PROJECT_ENVIRONMENT
}

bool GbmpDrawArcBy3PtsModelLineWithoutRefCmd::IsEnabled() const
{
    CMD_ONLY_ENABLE_IN_PROJECT_ENVIRONMENT
}

OwnerPtr<IAction> GbmpDrawArcBy3PtsModelLineWithoutRefCmd::ExecuteCommand(const gcmp::CommandParameters& cmdParams)
{
    ActionDrawArcBy3PtsElementInput actionInput;
    // 定制Action
    actionInput.CommandId = GetCommandId();
    actionInput.DrawArcMode = DrawArcBy3PointsMode::Chained;
    actionInput.ArcFinishedCallback = actionInput.CreateOnArcFinishedCallback(&GbmpDrawArcBy3PtsModelLineWithoutRefCmd::OnArcFinished, this);

    std::vector<Int64> priorityTypes;
    priorityTypes.emplace_back(typeid(IModelLine).hash_code());
    actionInput.SnapCandidatesPreprocessor = NEW_AS_OWNER_PTR(GbmpSnapEndPointPriorityTypesPreprocessor, priorityTypes);

    OwnerPtr<IAction> opAction = ActionDrawArcBy3PtsElementCreator::Create(actionInput);

    // 开启动态上下文面板
    AddGbmpDynamicPannelUtils::SetupModelLineCreationContextWithoutRef(opAction.get(), ID_PAGE_GBMP_MODIFY);

    return TransferOwnership(opAction);
}

ElementId GbmpDrawArcBy3PtsModelLineWithoutRefCmd::OnArcFinished(gcmp::IUiView* pCurrentView, const gcmp::Vector3d& startPt, const gcmp::Vector3d& endPt,
    const gcmp::Vector3d& onArcPt, const gcmp::ISnap* pStartSnap, const gcmp::ISnap* pEndSnap)
{
    DBG_WARN_AND_RETURN_UNLESS(pCurrentView, ElementId::InvalidID, L"pCurrentView为空",L"GDMPLab",L"2024-03-30");

    IDocument* pDoc = UiDocumentViewUtils::GetCurrentDocument();
    DBG_WARN_AND_RETURN_UNLESS(pDoc, ElementId::InvalidID, L"pDoc为空",L"GDMPLab",L"2024-03-30");
    
    IModelView* pModelView = UiDocumentViewUtils::GetCurrentModelView();
    DBG_WARN_AND_RETURN_UNLESS(pModelView, ElementId::InvalidID, L"pModelView为空",L"GDMPLab",L"2024-03-30");

    OwnerPtr<IUserTransaction> opTransaction = IUserTransaction::Create(pDoc, GBMP_TR(L"绘制三点圆弧模型线"));
    DBG_WARN_AND_RETURN_UNLESS(opTransaction.get(), ElementId::InvalidID, L"opTransaction为空",L"GDMPLab",L"2024-03-30");
   
    OwnerPtr<IArc3d> opCurve = IArc3d::Create(startPt, endPt, onArcPt);
    IModelLine* pModelLine = IModelLine::Create(pDoc, TransferOwnership(opCurve), ElementCreationOptions::Normal);

    // 圆弧可能创建失败
    if (pModelLine == nullptr)
    {
        opTransaction->Rollback();
        return ElementId::InvalidID;
    }
    else
    {
        opTransaction->Commit();
        ElementId modelLineId = pModelLine->GetOwnerElement()->GetBasicInformation()->GetElementId();
        DBG_WARN_AND_RETURN_UNLESS(modelLineId.IsValid(), ElementId::InvalidID, L"modelLineId无效",L"GDMPLab",L"2024-03-30");
        return modelLineId;
    }
}
